package com.lecture.junit.tdd.example;

import java.util.concurrent.atomic.AtomicInteger;

public abstract class LoginServiceState {

	AtomicInteger count;
	final long EXPIRATION_TIME = 45L * 24 * 60 * 60 * 1000; // 45 days

	public void login(LoginServiceInterface context, IAccount acc,
			String password) {
		if (acc.passwordMatches(password)) {
			if (acc.isLoggedIn()) {
				throw new AccountAlreadyLoggedInException();
			}
			if (acc.isRevoked()) {
				throw new AccountIsRevokedException();
			}
			if (acc.isExpared()) {
				throw new PasswordExparedException();
			}
			System.out.println(acc.getLastTimeActivity());
			if ((acc.getLastTimeActivity() + EXPIRATION_TIME) < System
					.currentTimeMillis()) {
				acc.setRevoked();
				throw new AccountIsExparedException();
			}			
			acc.setLoggedIn();
			acc.setLastTimeActivity(System.currentTimeMillis());
			context.setState(acc.getUserName(), new AwaitFirstLoginAttempt());
		} else {
			handlePasswordFail(context, acc);
		}
	}

	protected abstract void handlePasswordFail(LoginServiceInterface context,
			IAccount acc);
}
